package com.mall.ums.controller;

import com.mall.model.domain.ums.Resource;
import com.mall.ums.service.ResourceService;
import com.mall.utils.PageResult;
import com.mall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author clutchcitylee
 * @create 2023-03-13-19:42
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 添加后台资源
     *
     * @param resource
     * @return
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody Resource resource) {
        resource.setCreateTime(new Date());
        resourceService.save(resource);
        return R.success("添加资源成功");
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<String> deleteById(@PathVariable Integer id) {
        resourceService.removeById(id);
        return R.success("删除资源成功");
    }

    /**
     * 分页模糊查询后台资源
     *
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R<PageResult> list(Integer categoryId, String nameKeyword, String urlKeyword, Integer pageNum, Integer pageSize) {
        PageResult pageResult = resourceService.list(categoryId, nameKeyword, urlKeyword, pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 查询所有后台资源
     *
     * @return
     */
    @GetMapping("/listAll")
    public R<List<Resource>> listAll() {
        List<Resource> resources = resourceService.list();
        return R.success(resources);
    }

    /**
     * 修改后台资源
     *
     * @param id
     * @param resource
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> updateById(@PathVariable Integer id, @RequestBody Resource resource) {
        resourceService.updateById(resource);
        return R.success("修改后台资源成功");
    }

    /**
     * 根据ID获取资源详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Resource> getById(@PathVariable Integer id) {
        Resource resource = resourceService.getById(id);
        return R.success(resource);
    }
}
